import { dateTimeValidator } from "@/utils/validator";

export interface ListParamsBase {
  // 当前第几页
  page?: number;
  // 一页多少数据
  limit?: number;
  // 是否分页
  isPage?: boolean;
  // 开始时间
  startTime?: string;
  // 结束时间
  endTime?: string;
}

export type ListParams<T = Record<string, any>> = ListParamsBase & T;

export const getDefaultListParams = <T = Record<string, any>>(
  target: {
    [key: string]: number | boolean | string;
  } = {}
): ListParams<T> => {
  const result = {} as ListParams<T>;

  result.page =
    parseInt(String(target.page)) >= 1 ? parseInt(String(target.page)) : 1;

  result.limit =
    parseInt(String(target.limit)) >= 1 ? parseInt(String(target.limit)) : 10;

  result.isPage = target.isPage !== "false" && target.isPage !== false;

  result.startTime = dateTimeValidator(String(target.startTime))
    ? String(target.startTime)
    : "";

  result.endTime = dateTimeValidator(String(target.endTime))
    ? String(target.endTime)
    : "";

  return result;
};
